const {
    JSONModel
} = require('../../../../../public/json-helper/JSONModel');

const {
    SharedEditorElementConstructor
} = require('./editor-element-constructor');

class EditorElement extends JSONModel {

    static className() {
        return 'EditorElement';
    }

    static localJSONObjectKeyFilter(key) {
        if (key === '_baDuJiHao') {
            key = '_baDu';
        } if (key === '_taBanJiHao') {
            key = '_taBan';
        } if (key === '_qiangRuoJiHao') {
            key = '_qiangRuo';
        } if (key === '_chanYinJiHao') {
            key = '_chanYin';
        } if (key === '_xianPuChanYinJiHaoUIObject') {
            key = '_xianPuChanYinUIObject';
        } if (key === '_jianPuChanYinJiHaoUIObject') {
            key = '_jianPuChanYinUIObject';
        } if (key === '_xianPuTaBanJiHaoUIObject') {
            key = '_xianPuTaBanUIObject';
        } if (key === '_jianPuTaBanJiHaoUIObject') {
            key = '_jianPuTaBanUIObject';
        } if (key === '_xianPuBaDuJiHaoUIObject') {
            key = '_xianPuBaDuUIObject';
        } if (key === '_jianPuBaDuJiHaoUIObject') {
            key = '_jianPuBaDuUIObject';
        } if (key === '_xianPuQiangRuoJiHaoUIObject') {
            key = '_xianPuQiangRuoUIObject';
        } if (key === '_jianPuQiangRuoJiHaoUIObject') {
            key = '_jianPuQiangRuoUIObject';
        }
        return key;
    }

    static localJSONObjectClassNameFilter(className) {
        if (className === 'MusicScoreYinFuTaBan') {
            className = 'MusicScoreTaBan';
        } if (className === 'UIBaDuJiHao') {
            className = 'UIBaDu';
        } if (className === 'UIQiangRuoJiHao') {
            className = 'UIQiangRuo';
        } if (className === 'UIChanYinJiHao') {
            className = 'UIChanYin';
        }
        return className;
    }

    /**
     * 用普通Object初始化一个本类对象
     * @param JSONObject 普通的Object
     * @param data 初始JSON数据
     * @param cb 解析过程的回调，解析过程中可动态调整指定key
     * @returns {*} {*} 本类对象/null
     */
    static objectFromJSONObject(JSONObject, data, cb) {
        //console.log('JSONObject--------->',JSONObject)
        if (JSONObject && typeof JSONObject === 'object') {
            let constructor = this.getConstructor(JSONObject);
            constructor && constructor.JSONObjectInterceptor && constructor.JSONObjectInterceptor(JSONObject);
            let obj = Array.isArray(JSONObject) ? [] : new constructor();
            Object.forEach(JSONObject, (value, key) => {
                // key = this.localJSONObjectKeyFilter(key);
                let newKey = typeof cb === 'function' ? cb(key, JSONObject, obj) : key;
                let setter = obj.__lookupSetter__(newKey);
                if (typeof value === 'object') {
                    value = this.objectFromJSONObject(value, data, cb);
                }
                setter ? setter.call(obj, value) : obj[key] = value;
            });
            obj.createdFromJSONObject && obj.createdFromJSONObject(JSONObject, data, cb);
            return obj;
        }else{
          return null;
        }
    }

    static getConstructor(JSONObject) {
        try {
            SharedEditorElementConstructor.initial();
            if (JSONObject._className) {
                // JSONObject._className = this.localJSONObjectClassNameFilter(JSONObject._className);
                return SharedEditorElementConstructor.constructors[JSONObject._className];
            } else {
                return JSONObject.constructor;
            }
        } catch (e) {
            return Object;
        }
    }

    static JSONObjectInterceptor(JSONObject) {
    }

    /**
     * 这个方法用来监听当前对象初始化完毕并且属性已经全部从本地JSON数据中赋值完成
     *  ！！如果在特定子类中等待数据赋值完成，并且要做额外的数据处理时，可在子类重写此方法！！
     * @param JSONObject 本地对应的JSON对象
     * @param data       初始化的json数据
     * @param cb 当前JSON对象处理过程的回调
     */
    createdFromJSONObject(JSONObject, data, cb) {}

    /**
     * 用来存储每个元素全局的唯一标识
     * @returns {*}
     */
    get id() {
        return this._id;
    }

    set id(value) {
        this._id = value;
    }

    clone() {
        return Object.clone(this);
    }

    applyUpdate(command) {}
    unapplyUpdate(command) {}

    removeFromParent(parent) {}

    static remove(targets) {}

    onRemove() {}
}

module.exports = {
    EditorElement
};